use strict;
use Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = "1";
%IRSSI = (
    authors     => "Simon Ward",
    contact	=> "simon\@bleah.co.uk", 
    name        => "Greynick",
    description => "grey out lines written by nick",
    license	=> "GPL",
    changed	=> "2008-05-26T23:15+0100"
);

my $greyfile = "$ENV{HOME}/.irssi/greylist";
my %saved_greylist;
my %session_greylist = {};

sub load_greylist {
    open GREYLIST, $greyfile;
    while (<GREYLIST>) {
        s/#.*$//;            # strip comment
        next if /^(\s)*$/;  # skip blank line
        chomp;
        $saved_greylist{$_} = 1;
    }
    close GREYLIST;
}

sub save_greylist {
    open GREYLIST, ">$greyfile";
    foreach my $nick (keys %saved_greylist) {
        print GREYLIST "$nick\n";
    }
    close GREYLIST;
}

# Keep track of nick changes per session
sub sig_event_nick {
    my ($server, $newnick, $nick, $address) = @_;

    $newnick = substr ($newnick, 1) if ($newnick =~ /^:/);
    if ($saved_greylist{$nick} or $session_greylist{$nick}) {
        $session_greylist{$newnick} = 1;
    }
}

sub sig_message_public {
    my ($server, $msg, $nick, $addr, $target) = @_;
    my $chanrec = $server->channel_find($target);
    return unless $chanrec;
    my $nickrec = $chanrec->nick_find($nick);
    return unless $nickrec;

    if ($saved_greylist{$nick} or $session_greylist{$nick}) {
        $server->command('/^format pubmsg %K{pubmsgnick $2 {pubnick %K$0}}$1');
        $server->command('/^format pubmsg_me %K{pubmsgmenick $2 {menick %y$0}}$1');
        $server->command('/^format pubmsg_hilight %K{pubmsghinick $0 $3 %y$1}$2');
    } else {
        $server->command('/^format -reset pubmsg');
        $server->command('/^format -reset pubmsg_me');
        $server->command('/^format -reset pubmsg_hilight');
    }
}

sub sig_message_irc_action {
    my ($server, $msg, $nick, $addr, $target) = @_;
    my $chanrec = $server->channel_find($target);
    return unless $chanrec;
    my $nickrec = $chanrec->nick_find($nick);
    return unless $nickrec;

    if ($saved_greylist{$nick} or $session_greylist{$nick}) {
        $server->command('/^format action_public %K{pubaction %K$0}$1');
    } else {
        $server->command('/^format -reset action_public');
    }
}

sub cmd_greylist_load {
    load_greylist;
}

sub cmd_greylist_save {
    save_greylist;
}

sub cmd_greylist_add {
    my ($nick) = $_[0];
    if ($nick) {
        $saved_greylist{$nick} = 1;
    } else {
        Irssi::print('No nick given');
    }
}

sub cmd_greylist_remove {
    my ($nick) = $_[0];
    if ($nick) {
        delete ($saved_greylist{$nick});
    } else {
        Irssi::print('No nick given');
    }
}

sub cmd_greylist_show {
    Irssi::print("\nSaved greylist:");
    foreach my $nick (keys %saved_greylist) {
        Irssi::print($nick);
    }
    Irssi::print("\nSession greylist:");
    foreach my $nick (keys %session_greylist) {
        Irssi::print($nick);
    }
}

sub cmd_greylist {
    my ($data, $server, $witem) = @_;
    chomp($data);
    $data = $data || 'show';
    Irssi::command_runsub('greylist', $data, $server, $witem);
}

load_greylist;

Irssi::command_bind('greylist', 'cmd_greylist');
Irssi::command_bind('greylist load', 'cmd_greylist_load');
Irssi::command_bind('greylist save', 'cmd_greylist_save');
Irssi::command_bind('greylist add', 'cmd_greylist_add');
Irssi::command_bind('greylist remove', 'cmd_greylist_remove');
Irssi::command_bind('greylist show', 'cmd_greylist_show');

Irssi::signal_add('message public', 'sig_message_public');
Irssi::signal_add('message irc action', 'sig_message_irc_action');
Irssi::signal_add('event nick', 'sig_event_nick');
